#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2012/02/13

Module with the class that creates the html/csv files requested in the program

@author: João Vitor Machado Batista nº 5479
'''

import csv
import time

class FilesMaker:


    def csvFileCreator(self, paramList, statisticList):

        '''
            Creates a csv file with the given paramethers and the list used
            to calculate the statistics
        '''

        param = paramList
        data = statisticList

        fileName = param[0]
        
        csvFile = open(fileName, 'wb')
        fileStorage = csv.writer(csvFile, delimiter=',')

        fileStorage.writerow([param[1]])
        fileStorage.writerow([])

        a = 3
        while( a <= len(param) -1):
            fileStorage.writerow([param[a], param[a+1]])
            a = a + 2

        fileStorage.writerow([])
	fileStorage.writerow([param[2]])
	fileStorage.writerow([])
        
        for b in range (0, len(data)):
            fileStorage.writerow([b+1 ,data[b].encode('utf-8')])
            pass

        csvFile.close()


    def htmlIndexFileCreator(self):

        '''
            Creates the index file used in the server
        '''
        
        indexfile = open('index.html', "wb")

        htmlCode = '''<!DOCTYPE HTML>
                    <html>

                    <head>
                    <title>Index</title>
                    <meta name="description" content="Index for the Application generated html code" />
                    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                    </head>
                    <body>
                    </br>
                    <div align="center"><h1>Index</h1></div>
                    </br>
                    <div align="center"><h2>Statistics generated by the user</h2></div>
                    </br>
                    <div align="center"><h3><a href="html/schoolsPerYear.html" target = "blank">#1 - Schools per year</a></h3></div>
                    </br>
                    <div align="center"><h3><a href="html/profsPerYearSchoolCat.html" target = "blank">#2 - Teachers per year, school and category</a></h3></div>
                    </br>
                    <div align="center"><h3><a href="html/profsPerYearSchoolCategoryDegree.html" target = "blank">#3 - Teachers per year, school, category and degree</a></h3></div>
                    </br>
                    <div align="center"><h2>Automaticly generated statistics(All combinations possible within a year)</h2></div>
                    <div align="center"><h2>WARNING:The files may take a few minutes to open due to the size. Procede with caution!!</h2></div>
                    </br>
                    <div align="center"><h3><a href="html/statistic_1_all.html" target = "blank">#4 - Schools(All the schools in Portugal from 2000 to 2010)</a></h3></div>
                    </br>
                    <div align="center"><h3><a href="html/statistic_2_all.html" target = "blank">#5 - Teachers per year, school and category</a></h3></div>
                    </br>
                    <div align="center"><h3><a href="html/statistic_3_all.html" target = "blank">#6 - Teachers per year, school, category and degree</a></h3></div>
                    </body>
                    </html>'''

        indexfile.write(htmlCode)
        indexfile.close()

        pass

    def htmlFileCreator(self, paramList, statisticList):

        '''
            Creates a html file with the given paramethers and the list used
            to calculate the statistics
        '''

        param = paramList
        data = statisticList

        fileName = param[0]
        
        filehtml = open(fileName, 'wb')

        # string with the html head code
        htmlCode1 = '''<!DOCTYPE HTML>
                    <html>
                    <head>
                    <title>{0}</title>
                    <meta name="description" content="{0}" />
                    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                    </head>
                    <body>
                    </br>
                    <div align="center"><h1>{0}</h1></div>
                    </br>
                    '''.format(param[1].encode('utf-8'))
        
        filehtml.write(htmlCode1)                      

        # string with the data from a given statistic
        a = 3
        while( a <= len(param) -1):
            htmlCode2 = '''<div align="center"><h4>{0}{1}</h4></div>'''.format(param[a], param[a+1])
            filehtml.write(htmlCode2)
            a = a + 2

        htmlCode3 = '''</br>
                       <div align="center"><h2>{0}:</a></h2></div>'''.format(param[2])
        filehtml.write(htmlCode3)

        # string with a list line
        for x in range (0, len(data)):
            htmlCode4 = '''<div align="center"><h5>#{0} - {1}</h5></div>'''.format(x+1, data[x].encode('utf-8'))
            filehtml.write(htmlCode4)


        htmlCode5 = '''</body>
                    </html>'''

        filehtml.write(htmlCode5)
        filehtml.close()
        pass
    
